package cn.im.controller;

import cn.im.domain.Greeting;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.atomic.AtomicLong;

/**
 * RESTful风格 API
 * 	 * RESTful风格 GET（SELECT）：从服务器取出资源（一项或多项）。
 * 	 * POST（CREATE）：在服务器新建一个资源。
 * 	 * PUT（UPDATE）：在服务器更新资源（客户端提供改变后的完整资源）。
 * 	 * PATCH（UPDATE）：在服务器更新资源（客户端提供改变的属性）。
 * 	 * DELETE（DELETE）：从服务器删除资源。
 * @author Administrator
 *
 */
@RestController  // every method returns a domain object instead of a view,shorthand for including both @Controller and @ResponseBody.
public class GreetingController {
	
	private static final String template = "Hello, %s!";
	private final AtomicLong counter = new AtomicLong();


	@GetMapping(value = "/greeting")  //@RequestMapping(method=GET)
	@ResponseStatus(HttpStatus.OK)
	public Greeting greeting(@RequestParam(value = "name",defaultValue = "world") String name) {
		//Spring’s MappingJackson2HttpMessageConverter is automatically chosen to convert the Greeting instance to JSON.
		return new Greeting(counter.incrementAndGet(),String.format(template,name));
	}

/*	@PostMapping(value = "/users")
	@ResponseStatus(HttpStatus.CREATED)
	public Object addUser1(@RequestBody User user) {
		return userService.addUser(user);
	}

	@GetMapping(value = "/users/{id}")
	@ResponseStatus(HttpStatus.OK)
	public Object getUser1(@PathVariable("id") String id){
		return userService.getUserById(id);
	}

	@DeleteMapping(value = "/users/{id}")
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void deleteUser(@PathVariable("id") String id) {
		userService.delUser(id);
	}

	@PatchMapping(value = "/users/{id}")
	@ResponseStatus(HttpStatus.CREATED)
	public User updateUser1(@PathVariable("id") String id, @RequestBody User user) {
		return userService.updateUser(id, user);
	}*/

	@RequestMapping("/test")
	public String index() {
		return "RESTful API test!";
	}
}
